#include <stdio.h>
#include <omp.h>

int main() {
    int x;
    x = 2;

    #pragma omp parallel num_threads(2) shared(x)
    {
        int thread_num = omp_get_thread_num();

        if (thread_num == 0) {
            // 线程 0 写入 x
            printf("Thread %d: Initial value of x is %d\n", thread_num, x);
            #pragma omp atomic write
            x = 5;
            printf("Thread %d: Setting x to 5\n", thread_num);
        } else {
            // 线程 1 读取 x
            int xval;
            #pragma omp atomic read
            xval = x;
            printf("Thread %d: Reading x, value is %d\n", thread_num, xval);
        }
    }

    #pragma omp barrier

    if (omp_get_thread_num() == 0) {
        printf("Thread %d: After barrier, x is %d\n", omp_get_thread_num(), x);
    } else {
        printf("Thread %d: After barrier, x is %d\n", omp_get_thread_num(), x);
    }

    return 0;
}
